package com.wc.算法基础课.D第四讲数学知识.扩展欧几里得算法.扩展欧几里得算法;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/22 21:40
 * @description https://www.acwing.com/problem/content/879/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int n, a, b;
    static int[] x = new int[1], y = new int[1];

    /**
     * 扩展欧几里得算法
     * 对于任意两个整数a,b，都存在一对x，y使得ax + by = gcd(a,b);
     * 这个公式是为什么呢
     * 因为a 是a和b的最大公约数的倍数，b是a和b的最大公约数的倍数
     * 所以ax + by 就是gcd(a,b)的倍数，所以ax + by最小就是gcd(a,b)，
     * 我们要找的就是这个能凑出最小值的这对x，y
     *
     **/

    /***
     * 由欧几里得算法得：gcd(a,b) = gcd(b,a % b)
     * 又扩展欧几里得算法得： ax + by = gcd(a,b),   bx + (a % b)y = gcd(a,a % b)
     * 为了方便，将y跟x每次递归都进行换位，因为我们欧几里得的gcd(a,b) = gcd(b,a % b)
     * b每次都会换到前面，所以我们跟着换，会省掉很多不必要的换位麻烦
     * 所以 by + (a % b)x = gcd(a,b)
     * 又因 (a % b ) = a - (a / b) * b
     * 所以 by + (a - (a / b) * b)x = gcd(a,b)
     * 整理可得 ax + b(y - (a / b) * x) = gcd(a,b)
     * 所以x还是本身，这就是x跟y每次换位的好处，不用进行变换
     * y = y - (a / b) * x 所以每次递归y减掉（a / b ） * x
     * **/

    public static void main(String[] args) {
        n = sc.nextInt();
        while (n-- > 0) {
            a = sc.nextInt();
            b = sc.nextInt();
            exgcd(a, b, x, y);
            out.println(x[0] + " " + y[0]);
        }
        out.flush();
    }

    static int exgcd(int a, int b, int[] x, int[] y) {
        if (b == 0) {
            x[0] = 1;
            y[0] = 0;
            return a;
        }
        int d = exgcd(b, a % b, y, x);
        y[0] = y[0] - a / b * x[0];
        return d;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
